/*
 *Copyright @2015 HanHai
 *
 *All rights reserved.
 */
package com.hanhai.zrb.backend.web.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.hanhai.common.web.controller.BaseController;
import com.hanhai.zrb.api.biz.service.SysDictionaryService;
import com.hanhai.zrb.model.common.SysDictionary;

@Controller
@RequestMapping("dictionary")
public class DictionaryController extends BaseController {
	
	@Resource
	private SysDictionaryService sysDictionaryService;
	
	@RequestMapping("index")
	public String index() {
		return "dictionary/list";
	}

	@RequestMapping("list")
	@ResponseBody
	public String list(SysDictionary sysDictionary,Integer page,Integer rows) {
		PageInfo pageInfo = sysDictionaryService.findPageByKeyWord(sysDictionary, page, rows);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", pageInfo.getList());
		map.put("total", pageInfo.getTotal());
		return JSONObject.toJSONString(map);
	}
	
	@RequestMapping("initFirstLevelItem")
	@ResponseBody
	public String initFirstLevelItem() {
		List<SysDictionary> sysDictionaries = sysDictionaryService.getFirstLevelItem();
		SysDictionary sysDictionary = new SysDictionary();
		sysDictionary.setItemCode("0");
		sysDictionary.setItemName("无");
		sysDictionaries.add(0, sysDictionary);
		return JSONObject.toJSONString(sysDictionaries);
	}
	
	@RequestMapping(value="save",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String save(SysDictionary sysDictionary) {
		SysDictionary dic =sysDictionaryService.selectByCode(sysDictionary.getItemCode());
		Map<String, Object> map = new HashMap<String, Object>();
		if(null!=dic){
			map.put("success", false);
			map.put("infoMsg", "添加失败！");
		}else{
			sysDictionaryService.insertSelective(sysDictionary);
			map.put("msg", "添加成功！");
			map.put("success", true);
		}
		return JSONObject.toJSONString(map);
	}
	
	@RequestMapping(value="edit",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String edit(SysDictionary sysDictionary) {
		SysDictionary dic = sysDictionaryService.selectByCode(sysDictionary.getItemCode());
		Map<String, Object> map = new HashMap<String, Object>();
		if(null!=dic&&!dic.getId().equals(sysDictionary.getId())){
			map.put("success", false);
			map.put("infoMsg", "更新失败！");
		}else{
			sysDictionaryService.updateByPrimaryKeySelective(sysDictionary);
			map.put("msg", "更新成功！");
			map.put("success", true);
		}
		return JSONObject.toJSONString(map);
	}
	
	@RequestMapping("initByParentCode")
	@ResponseBody
	public String initByParentCode(String parentCode) {
		List<SysDictionary> list = Lists.newArrayList();
		if(StringUtils.isNotBlank(parentCode))
			list = sysDictionaryService.getItemByParentCode(parentCode);
		return JSONObject.toJSONString(list);
	}
	
	@RequestMapping("listAll")
	@ResponseBody
	public String listAll() {
		List<SysDictionary> list= sysDictionaryService.listAll();
		return JSONObject.toJSONString(list);
	}
	
	@RequestMapping("getDictionaryByParentCode")
	public void getDictionaryByParentCode(String parentCode,
			HttpServletResponse response) throws IOException {
		List<SysDictionary> childLs = sysDictionaryService
				.getItemByParentCode(parentCode);
		String str = JSONObject.toJSONString(childLs);
		response.getWriter().write(str);
	}

	@RequestMapping("getDictionaryNameByCode")
	public void getDictionaryNameByCode(String code,
			HttpServletResponse response) throws IOException {
		SysDictionary dictionary = sysDictionaryService.selectByCode(code);
		Map<String, Object> map = new HashMap<String, Object>();
		String itemName = "";
		if (null != dictionary) {
			itemName = dictionary.getItemName();
		}
		map.put("itemName", itemName);
		String str = JSONObject.toJSONString(map);
		response.getWriter().write(str);
	}
}
